package com.pmsystem.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.FileMapper;
import com.pmsystem.pojo.File;
import com.pmsystem.util.BeanUtil;

@Service
public class FileService {

	@Autowired
	private FileMapper fileMapper;

	public boolean uploadFile(String url, String fileName, Date date) {
		File file = new File();
		file.setFileUrl(url);
		file.setFileName(fileName);
		file.setUploadTime(date);
		int flag = fileMapper.insertSelective(file);
		return flag == 1 ? true : false;
	}

	public String download(Map<String, Object> map) {
		List<File> list = fileMapper.findFile(BeanUtil.mapToMapWithoutNull(map));
		if (list.size() == 1) {
			return list.get(0).getFileUrl() + "/" + list.get(0).getFileName();
		}
		return null;
	}
	
	public List<File> findFile(Map<String, Object> map){
		return fileMapper.findFile(BeanUtil.mapToMapWithoutNull(map));
	}

	public boolean update(Map<String, Object> map) {
		File file = new File();
		file.setFileId((Integer)map.get("fileId"));
		int flag =fileMapper.updateByPrimaryKeySelective(file);
		return flag==1?true:false;
	}

}
